package GUI.MenuPrincipal;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.MenuAdministracion;
import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import GUI.Cocina.MenuCocina;
import GUI.Ventas.MainVentas;
import employee.entities.EmployeeVO;
import employee.exceptions.IncompleteEmployeeException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.employee.interfaces.EmployeeProxyMgt;

public class Login {

	protected Shell shell;
	private Text text;
	private Text text_1;
	private Button btnNewButton;
	private Label error;
	Display display;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Login window = new Login();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(393, 177);
		shell.setText("SWT Application");
		
		Label lblCedula = new Label(shell, SWT.NONE);
		lblCedula.setBounds(43, 52, 123, 14);
		lblCedula.setText("Cedula");
		
		Label lblContrasena = new Label(shell, SWT.NONE);
		lblContrasena.setBounds(43, 91, 87, 14);
		lblContrasena.setText("Contrasena");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(137, 47, 155, 19);
		
		text_1 = new Text(shell, SWT.PASSWORD | SWT.BORDER);
		text_1.setBounds(136, 86, 156, 19);
		
		btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			int p=0;
			boolean err=false;
			try {
				p=Integer.parseInt(text.getText());
			}
			catch (Exception exc){
				err=true;
				error.setText("ingrese una cedula valida");
				
			}
			if(err==false){
				EmployeeProxyMgt uso = ServiceFacade.getInstance().getEmployeeProxyMgt();
				EmployeeVO usuario=null;
				try {
					usuario = uso.getEmployee(p);
					//System.out.println(usuario.getName());
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					//e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					//e1.printStackTrace();
				}
				if(usuario ==null){
					System.out.println("es nulo");
					error.setText("no se encontro ningun empleado con esa cedula");
				}
				else{
					String cont = text_1.getText();
					//System.out.println(cont);
					/*if(combo.getText().equalsIgnoreCase("Mozo")){
					op=0;
				}
				
				if(combo.getText().equalsIgnoreCase("Cajero")){
					op=1;
				}
				if(combo.getText().equalsIgnoreCase("Cocina")){
					op=2;
				}
				if(combo.getText().equalsIgnoreCase("Administrativo")){
					op=3;
				}*/
					
					try {
						boolean correcto = uso.authenticate(usuario, cont);
						//System.out.println(String.valueOf(correcto));
						if (correcto==true){
							if(usuario.getEmployeeType()==0){
								MenuCocina cocina = new MenuCocina (shell, shell.getStyle());
								//System.out.println("hasta aca");
								cocina.open();
								//display.close();
							}
							if(usuario.getEmployeeType()==1){
								MainVentas menu = new MainVentas(shell, shell.getStyle());
								menu.open();
								//shell.dispose();
							}
							if(usuario.getEmployeeType()==2){
								MenuCocina cocina = new MenuCocina(shell, shell.getStyle());
								cocina.open();
								//shell.close();
								
							}
							if(usuario.getEmployeeType()==3){
								MenuAdministracion menu = new MenuAdministracion(shell, shell.getStyle());
								menu.open();
								//shell.close();
							}
						}
						else{
							error.setText("El usuario y/o contrasena ingresados no son correctos");
						}
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (IncompleteEmployeeException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					}
				}
			}
			
			}
		});
		btnNewButton.setBounds(137, 117, 94, 28);
		btnNewButton.setText("Ingresar");
		
		error = new Label(shell, SWT.NONE);
		error.setBounds(43, 10, 276, 14);
		error.setText("");
		
		

	}
}
